<?php

namespace App\Http\Controllers\Api\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Api\Admin\BaseController;
use App\Http\Requests\Namecard\Store;
use App\Http\Requests\Namecard\Update;
use App\Models\Namecard;

class NamecardController extends BaseController
{
    private $return = array(
        'code' => 1,
        'msg' => ''
    );

    /**
     * [index description]
     * @param  Request  $request  [description]
     * @param  Namecard $namecard [description]
     * @return [type]             [description]
     */
    public function index(Request $request,Namecard $namecard){
        $pageSize = $request->input('pageSize',$this->pageSize);
        $map = $request->all();
        $result = $namecard->getDataByMap($map,$pageSize);
        $this->return['code'] = 0;
        $this->return['data'] = $result;
        return response()->json($this->return);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Store $request,Namecard $namecard)
    {
        $data = $request->all();
        $code = randCode(8,1);
        $data['code'] = $code;
        $result = $namecard->add_data($data);
        if(!$result){
            $this->return['msg'] = '操作失败！';
        }else{
            $this->return['data'] = array(
                'id' => hashid_encode($result)
            );
            $this->return['code'] = 0;
            $this->return['msg'] = '操作成功！';
            $this->return['data'] = array(
                'code' => $code
            );
        }
        return response()->json($this->return);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
     public function edit($id,Namecard $namecard)
     {
        $result = $namecard->getDataById($id);
        $this->return['code'] = 0;
        if(!$result){
            $this->return['msg'] = '查询失败！';
        }else{
            $this->return['code'] = 0;
            $this->return['data'] = $result;
        }
        return response()->json($this->return);
     }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Update $request,$id,Namecard $namecard)
    {
        $data = $request->all();
        $result = $namecard->edit_data($id,$data);
        if ($result) {
            $this->return['code'] = 0;
            $this->return['msg'] = '修改成功';
        } else {
            $this->return['msg'] = '修改失败';
        }
        return response()->json($this->return);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id,Namecard $namecard)
    {
        $result = $namecard->delData($id);
        if(!$result){
            $this->return['msg'] = '删除失败！';
        }else{
            $this->return['code'] = 0;
            $this->return['msg'] = '删除成功！';
        }
        return response()->json($this->return);
    }
}
